<template>
  <BaseModal :visible="visible" title="指标项选择框" :width="1200" cancelText="取消" okText="选中提交"
             @cancel="handleCancel" @ok="handleConfirm">
    <searchData :search-data="searchArr" ref="searchData" @search="search" />
    <s-table class="mb20" bordered ref="tableRef" tableLayout="fixed" size="default" rowKey="id"
             :columns="columns" :data="loadData" :alert="true" show-size-changer show-quick-jumper
             showPagination="auto" :row-selection="rowSelection" @on-success="onSuccess">
      ></s-table>
  </BaseModal>
</template>

<script>
import BaseModal from '@/components/BaseModal'
import Api from '@/api/science/base/quantitativeResponsibilityLetter'
import { STable } from '@/components'
import { validateShareExpire } from '@/api/bimIntegrate/business'
const columns = [
  {
    title: '目标量化要求',
    dataIndex: 'demand'
  },
  {
    title: '提炼指标',
    dataIndex: 'refiningTypeName'
  },
  {
    title: '目标量化对象',
    dataIndex: 'quantizationObjectTypeName'
  },
  {
    title: '内容(成果分类-成果等级)',
    dataIndex: 'content'
  }
]
export default {
  components: {
    BaseModal,
    STable
  },
  props: {
    addData: {
      type: Array,
      default: []
    },
    visible: {
      type: Boolean,
      default: ''
    }
  },
  data() {
    this.columns = columns
    return {
      queryParam: {}, // 搜索参数集
      searchArr: [
        {
          name: '目标量化要求',
          type: 'input',
          value: 'demand'
        },
        {
          name: '提炼指标',
          type: 'select',
          value: 'refiningTypeId',
          data: [],
          request: () => Api.typesLabelList({ type: 1 })
        },
        {
          name: '目标量化对象',
          type: 'select',
          value: 'quantizationObjectId',
          data: [],
          request: () => Api.typesLabelList({ type: 2 })
        },
        {
          name: '内容(成果分类-成果等级)',
          type: 'input',
          value: 'content',
        }
      ],
      selectedRowKeys: [],
      selectedRows: [],
      loading: {
        isFetching: false
      },
      formInfo: {
        modelId: ''
      },
      modelList: [],
      tableData: [],
      selectData: [], //选中的数据
      loadData: async params => {
        let searchData = Object.assign(params, { query: this.queryParam })
        searchData.query.stop = 1
        let res = await Api.queryList(searchData)
        if (res.code == '200' && res.data) {
          this.tableData = res.data || []
        }
        return Api.queryList(searchData)
      } // 表格请求
    }
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        selectedRows: this.selectedRows, // 当前行所有数据
        onChange: this.onSelectChange,
        // disabled:this.evaluateNames.includes(record.name) ? true:false,
        getCheckboxProps: (record) => ({
          props: {
            disabled: record.disabled
          }
        })
      }
    }
  },
  watch: {
    visible: {
      immediate: true,
      handler(val) {
        if (val) {
          this.setCheckStatus();
        }
      }
    }
  },
  methods: {
    onSuccess() {
      this.setCheckStatus();
    },
    setCheckStatus() {
      this.$nextTick(() => {
        const arr = []
        this.$refs.tableRef.localDataSource = this.$refs.tableRef.localDataSource.map(item => {
          if (this.addData.find((sub) =>
                  item.demand === sub.demand
                  && item.refiningTypeName === sub.refiningTypeName
                  && item.quantizationObjectTypeName === sub.quantizationObjectTypeName
                  && item.content === sub.content)) {
            arr.push(item.id)
            item.disabled = true;
          } else {
            item.disabled = false;
          }
          return item
        })
        this.$refs.tableRef.selectedRowKeys = arr;
        // this.selectedRows = [];
        // this.$refs.searchData.handleReset()
      })
    },
    /**
     * 选中
     * @type {string}
     */
    onSelectChange(e, row) {
      // let data = []
      // this.tableData.forEach(item => {
      //   e.forEach(sub => {
      //     if (item.id === sub) {
      //       data.push(item)
      //     }
      //   })
      // })
      this.selectData = row
    },
    /**
     * @description: 搜索
     * @param {*} val：搜索参数对象
     */
    search(val = {}) {
      this.queryParam = val;
      this.$refs.tableRef.refresh()
    },
    /**
     * @description: 提交模态框
     * @param {boolean} 是否需要刷新
     * @return {void}
     */
    handleConfirm() {
      if (this.selectData.length < 1) {
        this.$message.warning('请选择数据')
        return false
      }
      let arr =  this.selectData.filter(item => !item.disabled)

      this.$emit('selectData', arr)
      this.selectData = []
    },
    /**
     * @description: 关闭模态框
     * @param {boolean} 是否需要刷新
     * @return {void}
     */
    handleCancel() {
      this.$emit('selectData')
    }
  }
}
</script>

<style></style>
